# Copyright (C) 2007 Laszlo Pandy <laszlok2@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

import unittest
from AdventureMoney.core import Category, ShareProfile

class TestCase(unittest.TestCase):
	
	nameA = "Rent"
	nameB = "Utilities"
	personA = "Jim"
	personB = "Bob"
	shareA = ShareProfile.ShareProfile()
	shareB = ShareProfile.ShareProfile()
	
	def setUp(self):
		self.shareList = {
			"Bob" : (ShareProfile.SHARE_EQUAL, None),
			"Eve" : (ShareProfile.SHARE_PERCENT, 90),
			"Alice" : (ShareProfile.SHARE_EXACT, 4),
		}
		
		for name, (shareType, value) in self.shareList.iteritems():
			self.shareA.setPersonShare(name, shareType, value)
			self.shareB.setPersonShare(name, shareType, value)
		#make this one slightly different
		self.shareB.setPersonShare("monkey", ShareProfile.SHARE_EXACT, 10)
		
		self.categoryA = Category.Category(self.nameA, self.personA, self.shareA)
		self.categoryB = Category.Category(self.nameB, self.personB, self.shareB)
		self.categoryAdup = Category.Category(self.nameA, self.personA, self.shareA)
		
	def testRepr(self):
		try:
			repr(self.categoryA)
		except Exception, e:
			self.fail(repr(e))
			
	def testStr(self):
		self.assertEqual(str(self.categoryA), self.nameA)
		self.assertEqual(str(self.categoryB), self.nameB)
		self.assertEqual(str(self.categoryAdup), self.nameA)
		self.assertEqual(str(self.categoryAdup), str(self.categoryA))
		self.assertNotEqual(str(self.categoryB), self.nameA)
		
	def testCmp(self):
		# compare with each other
		self.assertEqual(self.categoryA, self.categoryAdup)
		self.assertNotEqual(self.categoryA, self.categoryB)
		self.assertNotEqual(self.categoryB, self.categoryAdup)
		self.assertNotEqual(self.categoryB, "")
		
		copyA = self.categoryA.copy()
		self.assertEqual(self.categoryA, copyA)
		copyA.getSharedBy().setPersonShare("gg", ShareProfile.SHARE_PERCENT, 99)
		self.assertNotEqual(self.categoryA, copyA)
		copyA.getSharedBy().setPersonShare("gg", ShareProfile.SHARE_NONE)
		self.assertEqual(self.categoryA, copyA)
		
		copyA.setPaidBy("monkey")
		self.assertNotEqual(self.categoryA, copyA)
		copyA.setPaidBy(self.categoryA.getPaidBy())
		self.assertEqual(self.categoryA, copyA)
		
		copyA.setPaidBy("monkey")
		self.assertNotEqual(self.categoryA, copyA)
		copyA.swapPerson("monkey", self.categoryA.getPaidBy())
		self.assertEqual(self.categoryA, copyA)
		
		#compare with other strings
		self.assertNotEqual(self.categoryA, self.nameA + "d")
		self.assertNotEqual(self.categoryB, self.nameB + "n")
	
	def testHash(self):
		hashA = hash(self.categoryA)
		hashB = hash(self.categoryB)
		hashAdup = hash(self.categoryAdup)
		
		self.assertEqual(hashA, hashAdup)
		self.assertNotEqual(hashB, hashA)
		self.assertNotEqual(hashAdup, hashB)
		
	def testCopy(self):
		copyA = self.categoryA.copy()
		copyB = self.categoryB.copy()
		
		self.assertEqual(self.categoryA, copyA)
		self.assertEqual(self.categoryB, copyB)
		self.assertEqual(copyA, self.categoryAdup)
		self.assertNotEqual(copyA, copyB)
		
	def testGetSetSharedBy(self):
		oldProfile = self.categoryA.getSharedBy()
		newProfile = ShareProfile.ShareProfile()
		self.categoryA.setSharedBy(newProfile)
		get = self.categoryA.getSharedBy()
		self.assertEqual(newProfile, get)
		
		self.categoryA.setSharedBy(oldProfile)
		get = self.categoryA.getSharedBy()
		self.assertEqual(oldProfile, get)
	
	def testIsSharedBy(self):
		self.assert_(self.categoryA.isSharedBy("Bob"))
		self.assert_(self.categoryA.isSharedBy("Alice"))
		self.failIf(self.categoryA.isSharedBy("zabo"))
		
		self.categoryA.getSharedBy().setPersonShare("zabo", ShareProfile.SHARE_NONE)
		self.failIf(self.categoryA.isSharedBy("zabo"))
		
		self.categoryA.getSharedBy().setPersonShare("zabo", ShareProfile.SHARE_EQUAL)
		self.assert_(self.categoryA.isSharedBy("zabo"))
		
		self.categoryA.getSharedBy().setPersonShare("zabo", ShareProfile.SHARE_NONE)
		self.failIf(self.categoryA.isSharedBy("zabo"))
		
	def testGetName(self):
		self.assertEqual(self.categoryA.getName(), self.nameA)
		self.assertEqual(self.categoryB.getName(), self.nameB)
		self.assertEqual(self.categoryAdup.getName(), self.nameA)
		self.assertNotEqual(self.categoryA.getName(), self.nameB)
		
	def testGetPaidBy(self):
		self.assertEqual(self.categoryA.getPaidBy(), self.personA)
		self.assertEqual(self.categoryB.getPaidBy(), self.personB)
		self.assertEqual(self.categoryAdup.getPaidBy(), self.personA)
		self.assertNotEqual(self.categoryA.getPaidBy(), self.personB)
		
	
	def tearDown(self):
		pass
